
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;
using UMC.Data;
namespace UMC.Net
{
    public class NetHttpRequest
    {
        public NetHttpRequest(Uri uri, ByteChunk memory)
        {
            this.Address = uri;
            this.RawUrl = memory.Rent(uri.PathAndQuery);
            this.Method = HttpMethod.GET;
            this._headValues[StringValue.Host] = new StringValue(uri.Authority.UTF8());
            this._Memory = memory;
        }
        public NetHttpRequest(Uri uri, StringValue rawUrl, ByteChunk memory)
        {
            this.Address = uri;
            this.RawUrl = rawUrl;
            this.Method = HttpMethod.GET;
            this._headValues[StringValue.Host] = new StringValue(uri.Authority.UTF8());
            this._Memory = memory;
        }
        ByteChunk _Memory;
        public ByteChunk Memory => _Memory;
        HeadValues _headValues = new HeadValues();
        public HttpMethod Method
        {
            get; set;
        }
        public Socks5 Proxy
        {
            get; set;
        }
        public StringValue RawUrl
        {
            get;
            set;
        }

        public Uri Address { get; private set; }

        public HeadValues Headers
        {
            get
            {
                return _headValues;
            }
        }
        public void Add(HeadValue headValue)
        {
            _headValues.Add(headValue);
        }

        public void Add(String name, String value)
        {
            this.Add(new HeadValue(name, value));
        }

        public int Timeout { get; set; }
        public long ContentLength
        {
            get
            {
                if (UMC.Data.Utility.TryParse(this.Headers.Get(StringValue.ContentLength.Span).Span, 10, out var v))
                {
                    return v;
                }
                return -1L;
            }
            set
            {
                _headValues[StringValue.ContentLength] = new StringValue(UTF8Encoding.UTF8.GetBytes(value.ToString()));
            }
        }
        public StringValue ContentType
        {
            get
            {
                return this.Headers.Get(StringValue.ContentType.Span);
            }
            set
            {
                _headValues[StringValue.ContentType] = value;

            }
        }
    }
}